﻿//-----------------------------------------------------------------------
// <copyright file="HttpClientTester.cs" company="北京正奇联讯科技有限公司">
//      Copyright 2018, ZhengQi (Beijing) Video Technology Co.,Ltd. ALL RIGHTS RESERVED 
//   Permission is hereby granted to licensees of ZhengQi (Beijing) Video Technology Co.,Ltd. 
//   products to use or abstract this computer program for the sole purpose of implementing 
//   a product based on ZhengQi (Beijing) Video Technology Co.,Ltd.products. No other rights to 
//   reproduce, use, or disseminate this computer program,whether in part or in whole, are granted. 
//   ZhengQi (Beijing) Video Technology Co.,Ltd. makes no representation or warranties with 
//   respect to the performance of this computer program, and specifically disclaims any 
//   responsibility for any damages, special or consequential, connected with the use of 
//   this program. For details, see http://www.cdv.com/
// </copyright>
// <summary>
//    HttpClient测试类
// </summary>
// <author>HouBaoZhu</author>
// <createTime>2018/03/21 13:52:03</createTime>
// <version>1.0</version>
// <modify></modify>
//-----------------------------------------------------------------------

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace CDV.IRMS.WebAPI.TaskExecution.XUnitTest
{
    /// <summary>
    /// HttpClient测试类
    /// </summary>
    internal class HttpClientTester
    {
        /// <summary>
        /// 任务执行WebApi基地址
        /// </summary>
        private const string ServerUrl = "http://172.16.3.12:7777/";

        /// <summary>
        /// 针对非检索功能的测试
        /// </summary>
        /// <param name="action">执行Api接口的回调</param>
        internal static void RunForNonQuery(Func<HttpClient, Task<HttpResponseMessage>> action)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(ServerUrl);
                var task = action.Invoke(httpClient);

                if (task.Result.StatusCode == HttpStatusCode.OK)
                {
                    var jsonResult = task.Result.Content.ReadAsStringAsync().Result;
                    var result = JsonConvert.DeserializeAnonymousType(jsonResult,
                       new { TaskID = 0, RouteID = 0, ErrorMsg = string.Empty });

                    Assert.True(string.IsNullOrEmpty(result.ErrorMsg), result.ErrorMsg);
                }
            }
        }

        /// <summary>
        /// 针对检索功能的测试
        /// </summary>
        /// <param name="action">执行Api接口的回调</param>
        internal static void RunForQuery(Func<HttpClient, Task<HttpResponseMessage>> action)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(ServerUrl);
                var task = action.Invoke(httpClient);

                if (task.Result.StatusCode == HttpStatusCode.OK)
                {
                    var jsonResult = task.Result.Content.ReadAsStringAsync().Result;
                    var result = JsonConvert.DeserializeAnonymousType(jsonResult,
                        new { RouteList = new List<Model.TaskRouteInfoModel>(), ErrorMsg = "" });

                    Assert.True(string.IsNullOrEmpty(result.ErrorMsg), result.ErrorMsg);
                    Assert.NotEmpty(result.RouteList);
                }
            }
        }
    }
}
